package com.huizhonglm.diap.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.PatternUitl;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.DialogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.presenters.user.PasswdModifyPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.PasswdModifyIView;


/**
 * 修改密码
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2013年8月2日 下午1:06:15 (1.0)
 */
public class PasswdModifyActivity extends BaseActivity implements PasswdModifyIView, View.OnClickListener {
    /**
     * 旧密码
     */
    private EditText oldPwdEdt = null;
    /**
     * 新密码
     */
    private EditText newPwdEdt = null;
    /**
     * 重复密码
     */
    private EditText repPwdEdt = null;
    /**
     * 逻辑处理器
     */
    private PasswdModifyPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         // 友盟错误报告统计
        setContentView(R.layout.activity_passwd_modify);
        setBackEvent(this, null);
        mPresenter = new PasswdModifyPresenter(this);
        assignViews();
    }

    @Override
    protected void assignViews() {
        Button modifyBtn = (Button) findViewById(R.id.modify_pwd_btn);
        oldPwdEdt = (EditText) findViewById(R.id.old_passwd_edt);
        newPwdEdt = (EditText) findViewById(R.id.new_passwd_edt);
        repPwdEdt = (EditText) findViewById(R.id.rep_passwd_edt);
        modifyBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_pwd_btn:
                closeKeyboard(PasswdModifyActivity.this);
                String oldPwd = oldPwdEdt.getText().toString().trim();
                String newPwd = newPwdEdt.getText().toString().trim();
                String repPwd = repPwdEdt.getText().toString().trim();
                if (checkPwd(oldPwd, newPwd, repPwd)) {
                    mPresenter.modifyPasswd(oldPwd, newPwd);
                }
                break;
        }
    }

    @Override
    public void onModifySuccess() {
        showTipDialog("密码修改成功", true);
    }

    @Override
    public void onModifyFailure(String errMsg) {
        showTipDialog(errMsg, false);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void showTipDialog(String text, final boolean success){
        DialogUtil.showConfirmDialog(this, "提示", text, new DialogUtil.OnDialogBtnClickListener() {
            @Override
            public void onBtnClick(String btnText) {
                if (success) {
                    onBackPressed();
                }
            }
        });
    }

    private boolean checkPwd(String oldPwd, String newPwd, String repPwd) {
        if (EmptyUtil.isEmpty(oldPwd)) {
            ToastUtil.makeTextShort("对不起，原密码有误");
            return false;
        }else {
            if (oldPwd.length() < 6) {
                ToastUtil.makeTextShort("对不起，原密码长度不够");
                return false;
            }
        }

        if (EmptyUtil.isEmpty(newPwd)) {
            ToastUtil.makeTextShort("对不起，密码不能为空");
            return false;
        }else {
            if (newPwd.length() < 6) {
                ToastUtil.makeTextShort("对不起，密码长度不够");
                return false;
            }
            if (PatternUitl.isPureNum(newPwd)) {
                ToastUtil.makeTextShort("对不起，密码不能为纯数字");
                return false;
            }
        }

        if (EmptyUtil.isEmpty(repPwd)) {
            ToastUtil.makeTextShort("对不起，两次输入的密码不一致");
            return false;
        }

        if (!newPwd.equals(repPwd)) {
            ToastUtil.makeTextShort("对不起，两次输入的密码不一致");
            return false;
        }
        return true;
    }
}
